import os
from django.conf import settings
from django.http import Http404
from django.shortcuts import render
from django.template import Template
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, HttpResponseForbidden
from django.utils._os import safe_join

def get_page_or_404(name):
	try:
	    file_path = safe_join(seeting.SITE_PAGE_DIRECTORY,name)
	except :
	    raise Http404('Page Not Found')
	else :
		if not os.path.exists(file_path):
		    raise Http404('Page Not Found')
	with open(file_path,'r') as f:
		page = Template(f.read())
	return page

def demo(request):
	return HttpResponse('demo - OK')

def login(request):
	context = {
		'slug':	'slug',
		'page':'login',
		'title':'乔安生',
		'year': 2019
	}
	return render(request,'login.html',context)

def page(request,slug='index'):
	file_name = '{}.html'.format(slug)
	page = 123 #get_page_or_404(file_name)
	context = {
		'slug':	slug,
		'page':page,
		'title':'乔安生',
		'year': 2019
	}
	return render(request,'page.html',context)
	#return HttpResponse('page - OK - ' + slug);